package cae.gui.util;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;

import cae.logic.util.CaeClipConfiguration;

public class CaeSoundUtil 
{
	public static final int POPUP_SOUND = 0;
	private static CaeSoundUtil instance = new CaeSoundUtil();
	private boolean soundEnabled;


	private CaeSoundUtil()
	{
		int soundE = CaeClipConfiguration.getInstance().getIProp(CaeClipConfiguration.SOUND_ENABLED);
		if( soundE != 0)
		{
			this.soundEnabled = true; 
		}
	}

	public static boolean isSoundEnabled()
	{
		return instance.soundEnabled;
	}

	public static void setSoundEnabled(boolean soundEnabled)
	{
		instance.soundEnabled = soundEnabled;

		int soundEnabledInt;
		if( instance.soundEnabled)
			soundEnabledInt = 1;
		else
			soundEnabledInt = 0;

		CaeClipConfiguration.getInstance().setIProp(CaeClipConfiguration.SOUND_ENABLED, soundEnabledInt);
	}

	public static void play(int idSound)
	{
		if(instance.soundEnabled)
		{
			switch (idSound) {
			case POPUP_SOUND:
				try {
					String popupSoundPath = CaeClipConfiguration.getInstance().getSProp(CaeClipConfiguration.POPUP_SOUND);
					AudioClip popupSound = Applet.newAudioClip(new File(popupSoundPath).toURI().toURL());
					popupSound.play();
				} catch (MalformedURLException e) {
					//  TODO
				}
				break;
			default:
				break;
			}

		}

	}


}
